<?php 
   // 货品列表
  class ShoplistController extends AuthController{
       
       private $model;
       // 自动运行方法
       public function __init(){
		parent::__init();
		$this->model=K('Shoplist');
	}

    // 显示货品列表
        public function index(){   
       $gid=Q('get.goods_id',0,'intval');
       $type_id=Q('get.type_id',0,'intval');
      // p($gid); p($type_id);die;
       //规格
     //通过type_id在类型属性表中查到该商品对应的规格名称
		$spec = K('Attribute')->where("class=1 AND type_id={$type_id}")->all();

		foreach ($spec as $k => $v) {
			$spec[$k]['option'] = K('GoodsAttr')->where("gid={$gid} AND attr_id={$v['attr_id']}")->all();

		}
			// p($spec[$k]['option']);die;
		$this->assign('spec',$spec);
		
		//获得该商品所填写的货品列表数据
		$data = $this->model->where("gid={$gid}")->all();
		foreach ($data as $k => $v) {
			$data[$k]['spec'] = K('GoodsAttr')->where("goods_att_id in (" . $v['combine'] . ")")->getField('attr_value',true);	
		}
 // p($data);die;
		$this->assign('data',$data);


           $this->display();


        }
   


public function add(){

 if(IS_POST){
    // 调用模型添加操作
    if(!$this->model->addData()){
      $this->error($this->model->error);
    }
    $this->success('添加成功');
   }

}

// 修改
public function edit(){
        $gtid=Q('get.gtid',0,'intval');
		$gid=Q('get.gid',0,'intval');
		$type_id=K('Goods')->where("goods_id={$gid}")->find();
		$type_id=$type_id['type_id'];
		if (IS_POST) {
			if (!$this->model->editData($gtid)) {
				$this->error($this->model->error);
			}
			$this->success("修改成功",U('index',array('gid'=>$gid,'type_id'=>$type_id)));
		}
		// 获取旧数据
		$olddata=$this->model->where("gtid={$gtid}")->find();
		$this->assign('olddata',$olddata);
		$this->display();
 

}

// 货品删除
public function del(){
  $gtid=Q('get.gtid',0,'intval');
  $this->model->where("gtid={$gtid}")->delete();
  $this->success('删除成功');


}


  }

 ?>

